/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package percussiongenerator.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import percussiongenerator.commands.CommandHistory;
import percussiongenerator.sound.IMidiPlayer;
import percussiongenerator.sound.MidiPlayerTimer;

/**
 * This panel is the main container. To use the application in an other
 * project add this panel to your project.
 * @author Jannes Plyson
 */
public class MainPanel extends JPanel{
    private IMidiPlayer midiPlayer;
    private TrackCollectionPanel trackCollectionPanel;


    /**
     * Constructor creates all necessary panels, and loads a midiplayer.
     * Loading a midiplayer takes time so this is done in an other thread.
     */
    public MainPanel(){
        this.setMinimumSize(new Dimension(800,500));
        this.setPreferredSize(new Dimension(800,500));        
        this.setLayout(new BorderLayout());
        final JLabel lblLoadingMidi = new JLabel("loading MIDI");
        lblLoadingMidi.setHorizontalAlignment(JLabel.CENTER);        
        this.add(lblLoadingMidi,BorderLayout.NORTH);
        final MainPanel thisObject = this;
        new Thread(new Runnable() {
            public void run() {
                midiPlayer = new MidiPlayerTimer();
                trackCollectionPanel = new TrackCollectionPanel(midiPlayer);
                midiPlayer.setTrackCollection(trackCollectionPanel.getTrackCollection());
                PlayerPanel playerPanel = new PlayerPanel(midiPlayer);
                thisObject.add(playerPanel,BorderLayout.NORTH);
                trackCollectionPanel.addToMidiPlayer();
                trackCollectionPanel.setBorder(BorderFactory.createTitledBorder("Tracks"));
                thisObject.add(trackCollectionPanel,BorderLayout.CENTER);
                JPanel southPanel = new JPanel();
                southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.X_AXIS));
                thisObject.add(southPanel,BorderLayout.SOUTH);
                JPanel inputPanel = new JPanel();
                inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.X_AXIS));
                inputPanel.setPreferredSize(new Dimension(250,200));
                southPanel.add(inputPanel);
                inputPanel.setBorder(BorderFactory.createTitledBorder("Input"));
                inputPanel.add(new TappingPanel(trackCollectionPanel.getTrackCollection(),trackCollectionPanel.getSelectedViewNode()));
                inputPanel.add(new MicrophoneTappingPanel(trackCollectionPanel.getTrackCollection()));
                inputPanel.add(new LoadFilePanel(trackCollectionPanel.getTrackCollection()));
                JPanel exportPanel = new JPanel();
                southPanel.add(exportPanel);
                exportPanel.setBorder(BorderFactory.createTitledBorder("Export"));
                JButton btnSave = new JButton("save");
                exportPanel.add(btnSave);
                btnSave.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        JFileChooser jfc = new JFileChooser();
                        int result = jfc.showSaveDialog(null);
                        if(result == JFileChooser.APPROVE_OPTION){
                            trackCollectionPanel.getTrackCollection().saveToXML(jfc.getSelectedFile());
                        }
                    }
                });
                thisObject.remove(lblLoadingMidi);                
                thisObject.revalidate();
            }
        }).start();        
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
            public boolean dispatchKeyEvent(KeyEvent e) {                
                if(e.isControlDown() && e.getKeyCode() == KeyEvent.VK_Z){                    
                    CommandHistory.undo();
                    return true;
                }
                return false;
            }
        });        
    }    

    
}
